#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2021-11-12 14:20
# @Author : huabao.tan
# @File : classify.py
# @Software: PyCharm

from PySide2.QtWidgets import QApplication, QMainWindow, QPushButton, QPlainTextEdit, QMessageBox
import time
import os
import shutil
import glob


class tidy():
    def __init__(self):
        self.window = QMainWindow()
        self.window.resize(600, 600)
        self.window.move(300, 300)
        self.window.setWindowTitle('文件整理')
        self.textEdit = QPlainTextEdit(self.window)
        self.textEdit.setPlaceholderText('请输入需要整理文件的路径')
        self.textEdit.move(10, 25)
        self.textEdit.resize(300, 25)
        self.butten = QPushButton('确定', self.window)
        self.butten.move(250, 300)
        self.butten.clicked.connect(self.handleCalc)

    def handleCalc(self):
        info = self.textEdit.toPlainText()
        mkdir_path = os.path.join(info, '文件分类')
        start_time = time.time()
        if not os.path.exists(mkdir_path):
            os.mkdir(mkdir_path)

        file_num = 0
        dir_num = 0
        for file in glob.glob(f'{info}\*'):
            print(file)
            if os.path.isfile(file):
                filename = os.path.basename(file)
                if '.' in filename:
                    suffix = filename.split('.')[-1]
                else:
                    suffix = 'others'
                if not os.path.exists(f'{mkdir_path}/{suffix}'):
                    os.mkdir(f'{mkdir_path}/{suffix}')
                    dir_num += 1
                    shutil.move(file, f'{mkdir_path}/{suffix}')
                    file_num += 1
        end_time = time.time()
        duaration_time = end_time - start_time
        if dir_num == 0:
            QMessageBox.about(self.window, '整理结果', f"""无事整理， 闲着没事？？？？""")
        else:
            QMessageBox.about(self.window, '整理结果', f'蹭蹭蹭的就整理完成了！！\n'
                              f'共有{file_num}个文件分类到了{dir_num}个文件中\n'
                              f'整理时间为{duaration_time}秒'
                              )

if __name__ == '__main__':
    app = QApplication([])
    stats = tidy()
    stats.window.show()
    app.exec_()